<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use EasyWeChat\Factory;
use EasyWeChat\Kernel\Support\generate_sign;
use Illuminate\Support\Facades\DB;
use App\Models\UsersWechat;
use App\Models\UserProfile;
use App\Models\Box;
use App\Models\LifeScene;
use App\Models\Comment;
use App\Models\Colour;
use App\Models\occupation;
use App\Models\position;
use App\Models\Feedback;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Hash;

class AccountController extends Controller
{


    //创建用户资料
    public function Create(Request $request){
        $wechat_id = 1;
        $Profile = UserProfile::where(['wechat_id'->$wechat_id])->first();
        if ($Profile) {
            if ($request->exists('life_id')) {
                $Profile->life_id = intval($request->input('life_id'));   //穿衣场景
            }
            if ($request->exists('figure')) {
                $Profile->figure = safe2($request->input('figure'));   //身形
            }
            if ($request->exists('special_take_care')) {
                $Profile->special_take_care = tirm(safe2($request->input('special_take_care')),'|');   //特别注意 |隔开
            }
            //身形大数据
            if ($request->exists('height')) {
                $Profile->height = intval($request->input('height'));//cm
            }
            if ($request->exists('weight')) {
                $Profile->weight = intval($request->input('weight'));//kg
            }
            //尺码
            if ($request->exists('clothes_size')) {
                $clothes_size = $request->input('clothes_size');   //尺码 数组传参 衬衣clothes_size['shirt'] 裤子clothes_size['trousers'] 鞋子clothes_size['shoes'] 其他clothes_size['other']
                $Profile->clothes_size =  serialize($clothes_size);
            }
            //颜色
            if ($request->exists('colour')) {
                $colour = safe2($request->input('colour'));   //颜色 数组传参参数对象是json
                $Profile->colour =  serialize($colour);
            }
            //偏好
            if ($request->exists('preferred_version')) {
                $preferred_version = safe2($request->input('preferred_version'));   //偏好 数组传参
                $Profile->preferred_version =  serialize($preferred_version);
            }

            if ($request->exists('photo')) {
                $Profile->photo = intval($request->input('photo'));   //全身照url
            }

            if ($request->exists('age')) {
                $Profile->age = intval($request->input('age'));   //年龄
            }
            if ($request->exists('birthday')) {
                $Profile->birthday = strtotime($request->input('birthday'));   //生日 转int
            }

            if ($request->exists('occupation_id')) {
                $Profile->occupation_id = safe2($request->input('occupation_id'));   //职业
            }
            if ($request->exists('position_id')) {
                $Profile->position_id = safe2($request->input('position_id'));   //职位
            }
            $Profile->save();
        }
        $this->PostBoxCreate($request);

        return $this->response('ok');
    }

    //完善盒子资料
    public function PostBoxCreate($request){
        $order_id = $request->input('order_id');
        $box_id = intval($request->input('box_id'));
        if ($request->exists('box_id')) {
            $box =  Box::find(intval($request->input('box_id')));
        }else{
            $box =  new Box;
            $box->number =  'box'.time();
            $box->order_id = $order_id;
        }
        if ($request->exists('life_id')) {
            $box->life_id = intval($request->input('life_id'));   //穿衣场景
        }
        if ($request->exists('figure')) {
            $box->figure = safe2($request->input('figure'));   //身形
        }
        if ($request->exists('special_take_care')) {
            $box->special_take_care = tirm(safe2($request->input('special_take_care')),'|');   //特别注意 |隔开
        }
        //身形大数据
        if ($request->exists('height')) {
            $box->height = intval($request->input('height'));//cm
        }
        if ($request->exists('weight')) {
            $box->weight = intval($request->input('weight'));//kg
        }
        //尺码
        if ($request->exists('clothes_size')) {
            $clothes_size = $request->input('clothes_size');   //尺码 数组传参 衬衣clothes_size['shirt'] 裤子clothes_size['trousers'] 鞋子clothes_size['shoes'] 其他clothes_size['other']
            $box->clothes_size =  serialize($clothes_size);
        }
        //颜色
        if ($request->exists('colour')) {
            $colour = safe2($request->input('colour'));   //颜色 数组传参参数对象是json
            $box->colour =  serialize($colour);
        }
        //偏好
        if ($request->exists('preferred_version')) {
            $preferred_version = safe2($request->input('preferred_version'));   //偏好 数组传参
            $box->preferred_version =  serialize($preferred_version);
        }

        if ($request->exists('photo')) {
            $box->photo = intval($request->input('photo'));   //全身照url
        }

        if ($request->exists('lately_photo')) {
            $box->lately_photo = intval($request->input('lately_photo'));   //近期资料 数组
        }

        if ($request->exists('age')) {
            $box->age = intval($request->input('age'));   //年龄
        }
        if ($request->exists('birthday')) {
            $box->birthday = strtotime($request->input('birthday'));   //生日 转int
        }

        if ($request->exists('occupation_id')) {
            $box->occupation_id = safe2($request->input('occupation_id'));   //职业
        }

        if ($request->exists('position_id')) {
            $box->position_id = safe2($request->input('position_id'));   //职位
        }
        if ($request->exists('remarks')) {
            $box->remarks = safe2($request->input('remarks'));   //备注
        }
        $box->save();
        return $this->response('ok');
    }

    //盒子发货
    public function boxSend(Request $request){
        $ids = explode('|', $request->input('ids'));
        foreach ($ids as $key => $id) {
            Box::where('id',$id)->update(['is_send'=>1]);
        }
        return $this->response('ok');
    }

    //添加地址
    public function CreateAddress(){

        $user = JWTAuth::parseToken()->authenticate();
        dd($user);
        $address = new Address;
        $address ->username= $request->input('username');
        $address ->province = $request->input('province');
        $address ->city = $request->input('city');
        $address ->area = $request->input('area');
        $address ->address = $request->input('address');
        $address ->phone = $request->input('phone');
        $address->save();
        if ($request->exists('box_ids')) {
            $box_ids = explode('|', $request->input('box_ids'));//可填
            Box::whereIn('id',$box_ids)->update(['send_address'=>$address->id]);
        }
        return $this->response('ok');
    }

    /**
     * 添加评分
     * @author wuyun
     * @date           2018-07-24T17:10:32+0800
     * @param  Request $request                 [description]
     * @return [type]                           [description]
     */
    public function postCreateComment(Request $request){
        $user = JWTAuth::parseToken()->authenticate();
        $evaluate = intval($request->input('evaluate',1));
        $score = intval($request->input('score',1));
        $clothing_score = $request->input('clothing_score');
        $comment = new Comment;
        $comment->evaluate= $evaluate;
        $comment->evaluate= $score;
        $comment->clothing_score= serialize($clothing_score);
        $comment->wechat_id = $user->id;
        $comment->save();
        return response()->json(['code' => 1,'msg' => 'Success','data' =>$comment]);
    }

    /**
     * 添加反馈
     * @author wuyun
     * @date           2018-07-24T17:10:32+0800
     * @param  Request $request                 [description]
     * @return [type]                           [description]
     */
    public function postCreateFeedback(Request $request){
        $user = JWTAuth::parseToken()->authenticate();
        $type = safe2($request->input('type'));//直接文字放进来
        $content = safe2($request->input('content'));
        $Feedback = new Feedback;
        $Feedback->type= $type;
        $Feedback->content= $content;
        $Feedback->wechat_id = $user->id;
        $Feedback->save();
        return $this->response('ok');
    }

    public function postCollectGoods(Request $request){
        $id = intval($request->input('id'));
        $box = Box::find($id);
        if (!$box) {
            return $this->error('盒子不存在');
        }
        $box->is_send=3;
        $box->Save();
        return $this->response('ok');
    }


}
